home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Extra 1997 #1 / Amiga Plus Extra 1997 #1.iso / programme / tools / leoutils / wc.c < prev    next >
C/C++ Source or Header  |  1994-11-21  |  3KB  |  129 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <exec/types.h>
  5. #if defined __SASC
  6.     #include <proto/dos.h>
  7. #else
  8.     #include <clib/dos_protos.h>
  9. #endif
  10. #include "LeoLib.h"
  11.  
  12.  
  13. static char *Version="$VER:Wc 1.0"
  14. #if defined __SASC
  15.     " "    "(21.11.94)" /*__AMIGADATE__*/ " ©1994 Leopold-Soft"
  16. #endif
  17.     ;
  18.  
  19.  
  20. static BOOL Wc(const char *Name);
  21.  
  22.  
  23.  
  24. #define TRUE 1
  25. #define FALSE 0
  26.  
  27. #define AP_LEN    1024
  28.  
  29. static ULONG Lines = 0, Words = 0, Chars = 0;
  30.  
  31. #define F_LINES     1
  32. #define F_WORDS     2
  33. #define F_CHARS     4
  34. #define F_TOTALONLY    8
  35.  
  36. int main(int argc, char **argv) {
  37.     int CurrArg = 0;
  38.     BOOL Flags = F_LINES|F_WORDS|F_CHARS;
  39.     ULONG TotalLines = 0, TotalWords = 0, TotalChars = 0;
  40.     struct AnchorPath *AP;
  41.  
  42.     if (!(AP = calloc(1, sizeof(struct AnchorPath) + AP_LEN))) return 30;
  43.     AP->ap_Strlen = AP_LEN;
  44.  
  45.     while(++CurrArg < argc) {
  46.         if (!strcmp(argv[CurrArg],"-l")) {
  47.             Flags &= ~(F_LINES | F_WORDS | F_CHARS);
  48.             Flags |= F_LINES;
  49.         } else if (!strcmp(argv[CurrArg],"-w")) {
  50.             Flags &= ~(F_LINES | F_WORDS | F_CHARS);
  51.             Flags |= F_WORDS;
  52.         } else if (!strcmp(argv[CurrArg],"-c")) {
  53.             Flags &= ~(F_LINES | F_WORDS | F_CHARS);
  54.             Flags |= F_CHARS;
  55.         } else if (!strcmp(argv[CurrArg],"-t")) {
  56.             Flags |= F_TOTALONLY;
  57.         } else if (!strcmp(argv[CurrArg],"-h") || !strcmp(argv[CurrArg], "?")) {
  58.             printf("Wc 1.0 by Henrik Herranen " __DATE__ "\n\n"
  59.                     "Usage: %s [?|-h] | [-l] | [-w] | [-c] | [-t] | files...\n\n",
  60.                     argv[0]);
  61.         } else {
  62.             if (MatchFirst(argv[CurrArg], AP)) {
  63.                 fprintf(stderr, "%s: Couldn't find %s!\n", argv[0], argv[CurrArg]);
  64.             } else {
  65.                 do {
  66.                     if (AP->ap_Info.fib_DirEntryType < 0) {
  67.                         if (!Wc(AP->ap_Buf)) {
  68.                             fprintf(stderr, "%s: Couldn't read %s!\n", argv[0], AP->ap_Buf);
  69.                         } else {
  70.                             TotalLines += Lines;
  71.                             TotalWords += Words;
  72.                             TotalChars += Chars;
  73.                             if (!(Flags & F_TOTALONLY)) {
  74.                                 printf("%-24s", AP->ap_Buf);
  75.                                 if (Flags & F_CHARS) printf(" %7ld", Chars);
  76.                                 if (Flags & F_WORDS) printf(" %6ld", Words);
  77.                                 if (Flags & F_LINES) printf(" %5ld", Lines);
  78.                                 printf("\n");
  79.                             }
  80.                         }
  81.                     } else {
  82.                         fprintf(stderr,"%s: Skipping directory \"%s\"\n", argv[0], AP->ap_Buf);
  83.                     }
  84.                 } while (!(MatchNext(AP)));
  85.                 MatchEnd(AP);
  86.             }
  87.         }
  88.     }
  89.  
  90.     if (!(Flags & F_TOTALONLY)) printf("\n%-24s", "GRAND TOTAL");
  91.     if (Flags & F_CHARS) printf(" %7ld", TotalChars);
  92.     if (Flags & F_WORDS) printf(" %6ld", TotalWords);
  93.     if (Flags & F_LINES) printf(" %5ld", TotalLines);
  94.     printf("\n");
  95.  
  96.     return 0;
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103. static BOOL Wc(const char *Name) {
  104.     FILE *InFile;
  105.     BOOL Inside = FALSE;
  106.     int InC;
  107.  
  108.     Lines = Words = Chars = 0;
  109.     if (!(InFile = fopen(Name, "r"))) return FALSE;
  110.  
  111.     while ((InC = getc(InFile)) != EOF) {
  112.         if (InC == '\n') Lines++;
  113.         if (IsAlnum(InC)) {
  114.             if (!Inside) {
  115.                 Inside = TRUE;
  116.                 Words++;
  117.             }
  118.         } else {
  119.             Inside = FALSE;
  120.         }
  121.         Chars++;
  122.     }
  123.  
  124.     if (Inside) Words++;
  125.     fclose(InFile);
  126.  
  127.     return TRUE;
  128.  }
  129.